package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.MallItemModel;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.view.activity.ActiveRankActivity;
import com.zw.yzk.kabu.view.activity.ExhibitionDetailsActivity;
import com.zw.yzk.kabu.view.activity.GoodsDetailsActivity;
import com.zw.yzk.kabu.view.activity.StoreActivity;
import com.zw.yzk.kabu.view.activity.WebActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/6.
 */

public class MallChildAdapter extends RecyclerView.Adapter<MallChildAdapter.Holder> {

    private List<MallItemModel.Item> goodList;
    private Context context;


    public MallChildAdapter(List<MallItemModel.Item> list) {
        this.goodList = new ArrayList<>();
        if (list != null) {
            this.goodList.addAll(list);
        }
    }

    public void refresh(List<MallItemModel.Item> list) {
        this.goodList.clear();
        if (list != null) {
            this.goodList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<MallItemModel.Item> list) {
        if (list != null) {
            int start = goodList.size();
            this.goodList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_mall_child, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final MallItemModel.Item item = goodList.get(position);
        holder.title.setText(item.getTitle());
        holder.content.setText(item.getSubTitle());

        //按照图片比例将图片宽放大/缩小到屏幕宽度
        Glide.with(context)
                .load(item.getImg())
                .asBitmap()
                .error(R.drawable.im_default)
                .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = (int) (PhoneUtils.getScreenWidth(context) - 24 * context.getResources().getDisplayMetrics().density);
                        int height = (int) (width * 4f / 7);

                        ViewGroup.LayoutParams params = holder.image.getLayoutParams();
                        if (params == null) {
                            params = new ViewGroup.LayoutParams(width, height);
                        } else {
                            params.height = height;
                            params.width = width;
                        }
                        holder.image.setLayoutParams(params);
                        holder.image.setImageBitmap(resource);
                    }
                });
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(item.getLinkUrl(), item.getTitle());
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.content)
        TextView content;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 打开指定的页面
     *
     * @param str 参数
     */
    private void startActivity(String str, String title) {
        try {
            if(str.startsWith("http")) {
                //打开url链接
                WebActivity.startWebActivity(context, title, str);
            } else if (str.startsWith("local")) {
                int id = Integer.parseInt(str.substring(str.lastIndexOf("=") + 1));
                if (str.contains("myshop")) {
                    Intent intent = new Intent(context, StoreActivity.class);
                    intent.putExtra(ActivityConstant.STORE_OWNER_ID, id);
                    context.startActivity(intent);
                } else if (str.contains("productdetail")) {
                    Intent intent = new Intent(context, GoodsDetailsActivity.class);
                    intent.putExtra(ActivityConstant.GOODS_DETAILS_ID, id);
                    context.startActivity(intent);
                } else if(str.contains("exhibitiondetail")) {
                    Intent intent = new Intent(context, ExhibitionDetailsActivity.class);
                    intent.putExtra(ActivityConstant.EXHIBITION_DETAILS_ID, id);
                    context.startActivity(intent);
                } else if(str.contains("activityrank")) {
                    ActiveRankActivity.startActiveRankActivity(context, id, title);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
